from django.db import models
from datetime import datetime
from choices import ACTION

class Attachment(models.Model):
    '''E-mail message attachment model'''
    
    size = models.IntegerField (max_length=11, null=False, blank=False)
    ctype = models.CharField (max_length=20, null=False, blank=False)
    filename = models.CharField (max_length=50)
    msg = models.ForeignKey ('Message')
    
class Header(models.Model):
    '''E-mail header model'''
    
    name = models.CharField (max_length=200, null=False, blank=False)
    value = models.CharField (max_length=1000, null=False, blank=False)
    msg = models.ForeignKey ('Message')

class Email(models.Model):
    '''E-mail address model'''
    
    name = models.CharField (max_length=45)
    address = models.EmailField (max_length=254)
    
    def __unicode__(self):
        txt = ""
        
        if self.name:
            txt = self.name + ' <'
        txt += self.address
        if self.name:
            txt += '>'
        return txt;
        
class Message(models.Model):
    '''E-mail message model'''
    
    size = models.BigIntegerField (blank=False, null=False)
    filepath = models.CharField (max_length=200, blank=True, null=True)
    came_date = models.DateTimeField (null=False, blank=False, default=datetime.now)
    sender = models.ForeignKey ('Email', related_name='sender')
    recipients = models.ManyToManyField ('Email', related_name='recipients')
    date = models.DateTimeField ()
    from_email = models.ForeignKey('Email', related_name='from')
    to = models.ManyToManyField ('Email', related_name='to')
    cc = models.ManyToManyField ('Email', related_name='cc')
    bcc = models.ManyToManyField ('Email', related_name='bcc')
    body = models.TextField ()
    subject = models.TextField ()
    action = models.IntegerField (choices=ACTION)    
        
    #action z filtr
    